package project;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.text.*;
import java.sql.Connection;

public class LookEmpPanel extends JPanel implements ActionListener
{
	private JPanel formP = null, buttonP = null, westP = null,
					eastP = null, searchP = null;
	private JButton editB = null, delB = null;
	JButton saveB = null;
	private JLabel searchL = null;
	private JTextField searchT = null;
	private JComboBox jcb = null;
	private JButton searchB = null;
	private JDialog empDialog = null;
	private JButton dialogAddB, dialogCancelB;
	private JTable empTable = null;
	DetailsPanel dp = null;
	AddressPanel ap1 = null;
	Employee emp;
	EmpManager em;
	Connection conn;
	
	public LookEmpPanel(Connection conn)
	{
		this.conn = conn;
		this.setLayout(new BorderLayout());
		
		dp = new DetailsPanel("employee","look");
		ap1 = new AddressPanel("Residential");
		buttonP = new JPanel();
		editB = new JButton("Edit details");
		editB.addActionListener(this);
		delB = new JButton("Delete");
		delB.addActionListener(this);
		saveB = new JButton("Save");
		saveB.addActionListener(this);
		buttonP.add(editB);
		buttonP.add(delB);
		buttonP.setBorder(BorderFactory.createEtchedBorder());
		formP = new JPanel(new BorderLayout());
		formP.add(dp, BorderLayout.NORTH);
		formP.add(ap1, BorderLayout.CENTER);
		westP = new JPanel(new BorderLayout());
		westP.add(formP, BorderLayout.CENTER);
		westP.add(buttonP, BorderLayout.SOUTH);
		westP.setPreferredSize(new Dimension(280,450));
		westP.setBorder(BorderFactory.createEmptyBorder(0,1,0,0));
		
		eastP = new JPanel(new BorderLayout());
		eastP.setPreferredSize(new Dimension(500,450));
		searchP = new JPanel();
		searchL = new JLabel("Search: ");
		searchT = new JTextField(40);
		String [] jcbString = new String[]{
				"Employee ID", "Last Name", "Phone No"};
		jcb = new JComboBox(jcbString);
		searchB = new JButton("Search");
		searchB.addActionListener(this);
		searchP.add(searchL);
		searchP.add(searchT);
		searchT.addActionListener(this);
		searchP.add(jcb);
		searchP.add(searchB);
		searchP.setBorder(BorderFactory.createEtchedBorder());
		//searchP.setBorder(BorderFactory.createCompoundBorder(
		//	BorderFactory.createEmptyBorder(10,10,10,10),
		//	BorderFactory.createRaisedBevelBorder()));
		
		//PhotoPanel pp = new PhotoPanel();
		
		//eastP.add(searchP, BorderLayout.NORTH);
		//eastP.add(pp, BorderLayout.SOUTH);
		
		this.add(searchP, BorderLayout.NORTH);
		this.add(westP, BorderLayout.CENTER);
		this.add(eastP, BorderLayout.EAST);
		
		setFieldsSelectable(false);
		dp.newPasswordB.addActionListener(this);
		dialogAddB = new JButton("SELECT");
		dialogAddB.addActionListener(this);
		dialogCancelB = new JButton("CANCEL");
		dialogCancelB.addActionListener(this);
		em = new EmpManager(conn);
		dp.empIDT.setBackground(new Color(173, 190, 230));
		searchT.requestFocus();
	}
	public DetailsPanel getDetailsPanel()
	{
		return dp;
	}
	public AddressPanel getAddressPanel()
	{
		return ap1;
	}
	public void setFieldsSelectable(Boolean tf)
	{
		if(tf)
		{
			dp.surnameT.setFocusable(true);
			dp.nameT.setFocusable(true);
			dp.initialsT.setFocusable(true);
			dp.idNumberT.setFocusable(true);
			dp.phoneOneT.setFocusable(true);
			dp.phoneTwoT.setFocusable(true);
			dp.emailT.setFocusable(true);
			dp.titleJCB.setFocusable(true);
			dp.accessLevelJCB.setFocusable(true);
			dp.jdc.setFocusable(true);
			ap1.streetT.setFocusable(true);
			ap1.suburbT.setFocusable(true);
			ap1.cityT.setFocusable(true);
			ap1.posCodeT.setFocusable(true);
		}
		else if(tf ==  false)
		{
			dp.empIDT.setFocusable(false);
			dp.surnameT.setFocusable(false);
			dp.nameT.setFocusable(false);
			dp.initialsT.setFocusable(false);
			dp.idNumberT.setFocusable(false);
			dp.phoneOneT.setFocusable(false);
			dp.phoneTwoT.setFocusable(false);
			dp.emailT.setFocusable(false);
			dp.titleJCB.setFocusable(false);
			dp.accessLevelJCB.setFocusable(false);
			dp.jdc.setFocusable(false);
			dp.newPasswordB.setEnabled(false);
			ap1.streetT.setFocusable(false);
			ap1.suburbT.setFocusable(false);
			ap1.cityT.setFocusable(false);
			ap1.posCodeT.setFocusable(false);
			
		}
	}
	public void emptyAll()
	{
		dp.empIDT.setText("");
		dp.surnameT.setText("");
		dp.nameT.setText("");
		dp.initialsT.setText("");
		dp.idNumberT.setText("");
		dp.phoneOneT.setText("");
		dp.phoneTwoT.setText("");
		dp.emailT.setText("");
		dp.titleJCB.setSelectedItem("");
		dp.accessLevelJCB.setSelectedItem("");
		dp.jdc.setDate(null);
		dp.newPasswordB.setEnabled(false);
		ap1.streetT.setText("");
		ap1.suburbT.setText("");
		ap1.cityT.setText("");
		ap1.posCodeT.setText("");
		searchT.setText("");
	}
	public void actionPerformed(ActionEvent e)
	{
		Object source = e.getSource();
		if(source == editB)
		{
			if(emp == null)
			{
				return;
			}
			setFieldsSelectable(true);
			delB.setEnabled(false);
			dp.validate();
			ap1.validate();
			buttonP.removeAll();
			buttonP.add(saveB);
			buttonP.add(delB);
			dp.surnameT.requestFocus();
			this.validate();
			this.repaint();
		}
		if(source == saveB)
		{
			
			Date date = dp.jdc.getDate();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String bdate = sdf.format(date);
			Date now = new Date();
			String jdate = sdf.format(now);
			String surname = dp.surnameT.getText();
			String [] attr = new String [17];
			attr[0] = dp.empIDT.getText();
			attr[1] = emp.getPassword();
			attr[2] = dp.idNumberT.getText();
			attr[3] = dp.surnameT.getText();
			attr[4] = dp.nameT.getText();
			attr[5] = dp.initialsT.getText();
			attr[6] = (String)dp.titleJCB.getSelectedItem();
			attr[7] = ap1.streetT.getText();
			attr[8] = ap1.suburbT.getText();
			attr[9] = ap1.cityT.getText();
			attr[10] = ap1.posCodeT.getText();
			attr[11] = dp.phoneOneT.getText();
			attr[12] = dp.phoneTwoT.getText();
			attr[13] = dp.emailT.getText();
			attr[14] = bdate;
			attr[15] = jdate;
			attr[16] = (String)dp.accessLevelJCB.getSelectedItem();
			
			emp = new Employee(attr);
			em.update(emp);
			buttonP.removeAll();
			buttonP.add(editB);
			buttonP.add(delB);
			this.validate();
			this.repaint();
			JOptionPane.showMessageDialog(this, "Saved");
			emptyAll();
		}
		if(source == dp.newPasswordB)
		{
			int choice;
			choice = JOptionPane.showConfirmDialog(this,
								"This will reset the employee password"+
								"\nAre you sure you wish to proceed?",
								"Password Reset",
								JOptionPane.OK_CANCEL_OPTION);
			if(choice == 0)
			{
				String pass = em.generateEmpPassword();
				String [] tempA = emp.getEmpAttrString();
				tempA[1] = pass;
				emp = new Employee(tempA);
				em.update(emp);
				String plainPass = em.getEmpPlainTextPassword();
				JOptionPane.showMessageDialog(this, 
								"The password was reset to: "+plainPass,
								"Reset successful",
								JOptionPane.INFORMATION_MESSAGE);
			}
			else
			{
				return;
			}
			
			
		}
		if((source == searchB)||(source == searchT))
		{
			String criteria = (String)jcb.getSelectedItem();
			System.out.println(criteria);
			
			String searchText = searchT.getText();
			System.out.println(searchText);
			if((criteria.equals("Last Name"))||(criteria.equals("Phone No")))
			{
				empTable = new JTable();
				empTable = em.searchEmp(searchText, criteria);
				if(empTable.getRowCount() > 1)
				{
					System.out.println("in row table");
					empDialog = new JDialog();
					empDialog.setLayout(new BorderLayout());
					JPanel dialogButtonPanel = new JPanel(new GridLayout(1,2));
					dialogButtonPanel.add(dialogAddB);
					dialogButtonPanel.add(dialogCancelB);
					JScrollPane empJSP = new JScrollPane(empTable);
					empJSP.setHorizontalScrollBarPolicy(
						ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
					empJSP.setVerticalScrollBarPolicy(
						ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
					empDialog.add(empJSP, BorderLayout.CENTER);
					empDialog.add(dialogButtonPanel, BorderLayout.SOUTH);
					empDialog.setSize(380,250);
					empDialog.setLocationRelativeTo(null);
					empDialog.setVisible(true);
					return;
				}
			
			}
			emp = em.findEmp(searchText, criteria);
			if(emp == null)
			{
				JOptionPane.showMessageDialog(this, 
						"Couldn't find employee", "Error", 
						JOptionPane.ERROR_MESSAGE);
				return;
			}
			dp.newPasswordB.setEnabled(true);
			
			dp.empIDT.setText(emp.getEmpId());
			dp.surnameT.setText(emp.getLastName());
			dp.nameT.setText(emp.getFirstName());
			dp.initialsT.setText(emp.getInitials());
			dp.idNumberT.setText(emp.getIdNum());
			dp.phoneOneT.setText(emp.getPhone1());
			dp.phoneTwoT.setText(emp.getPhone2());
			dp.emailT.setText(emp.getEmail());
			dp.titleJCB.setSelectedItem(emp.getTitle());
			dp.accessLevelJCB.setSelectedItem(emp.getAccessLevel());
			dp.jdc.setFocusable(false);
			try
			{
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				Date date = sdf.parse(emp.getBirthDate());
				dp.jdc.setDate(date);
			}
			catch(ParseException pe)
			{
				pe.printStackTrace();
			}
			
			ap1.streetT.setText(emp.getAddress1());
			ap1.suburbT.setText(emp.getAddress2());
			ap1.cityT.setText(emp.getAddress3());
			ap1.posCodeT.setText(emp.getAddress4());
			this.repaint();
			this.validate();
		}
		if(source == dialogAddB)
		{
			try
			{
				int rowIndex = empTable.getSelectedRow();
				searchT.setText((String)empTable.getValueAt(rowIndex,0));
				jcb.setSelectedItem("Employee ID");
				searchB.doClick();
				empDialog.dispose();
			}
			catch(Exception ex)
			{
				
			}
		}
		if(source == dialogCancelB)
		{
			empDialog.dispose();
		}
	}
}